
/**
 * 缓动动画类。支持的缓动函数如下：
 * @class 
 */
//https://easings.net/
const sin = Math.sin;
const cos = Math.cos;
const PI = Math.PI;
const pow = Math.pow;
const sqrt = Math.sqrt;
const easing = {
    none: function (t) {
        return t;
    },
    easeIn_Sine: function (t) {
        let p = 1 - cos((t * PI) / 2);
        return p;
    },
    easeIn_Cubic: function (t) {
        let p = pow(t, 3);
        return p;
    },
    easeIn_Quint: function (t) {
        let p = pow(t, 5);
        return p;
    },
    easeIn_Circ: function (t) {
        let p = 1 - sqrt(1 - pow(t, 2));
        return p;
    },
    easeIn_Elastic: function (t) {
        const c4 = (2 * PI) / 3;
        let p = t === 0
            ? 0
            : t === 1
                ? 1
                : -pow(2, 10 * t - 10) * sin((t * 10 - 10.75) * c4);
        return p;
    },
    easeIn_Quad: function (t) {
        let p = t * t
        return p;
    },
    easeIn_Quart: function (t) {
        let p = t * t * t * t;
        return p;
    },
    easeIn_Expo: function (t) {
        let p = t === 0 ? 0 : pow(2, 10 * t - 10);
        return p;
    },
    easeIn_Back: function (t) {
        const c1 = 1.70158;
        const c3 = c1 + 1;
        let p = c3 * t * t * t - c1 * t * t;
        return p;
    },
    easeIn_Bounce: function (t) {
        t = 1 - t;
        const n1 = 7.5625;
        const d1 = 2.75;

        if (t < 1 / d1) {
            t = n1 * t * t;
        } else if (t < 2 / d1) {
            t = n1 * (t -= 1.5 / d1) * t + 0.75;
        } else if (t < 2.5 / d1) {
            t = n1 * (t -= 2.25 / d1) * t + 0.9375;
        } else {
            t = n1 * (t -= 2.625 / d1) * t + 0.984375;
        }
        let p = 1 - t
        return p;
    },
    easeOut_Sine: function (t) {
        let p = sin((t * PI) / 2)
        return p;
    },
    easeOut_Cubic: function (t) {

        let p = 1 - pow(1 - t, 3);
        return p;
    },
    easeOut_Quint: function (t) {

        let p = 1 - pow(1 - t, 5);
        return p;
    },
    easeOut_Circ: function (t) {

        let p = sqrt(1 - pow(t - 1, 2));
        return p;
    },
    easeOut_Elastic: function (t) {

        const c4 = (2 * Math.PI) / 3;
        let p = t === 0
            ? 0
            : t === 1
                ? 1
                : pow(2, -10 * t) * sin((t * 10 - 0.75) * c4) + 1;
        return p;
    },
    easeOut_Quad: function (t) {

        let p = 1 - (1 - t) * (1 - t);
        return p;
    },
    easeOut_Quart: function (t) {

        let p = 1 - pow(1 - t, 4);
        return p;
    },
    easeOut_Expo: function (t) {

        let p = t === 1 ? 1 : 1 - pow(2, -10 * t);
        return p;
    },
    easeOut_Back: function (t) {

        const c1 = 1.70158;
        const c3 = c1 + 1;
        let p = 1 + c3 * pow(t - 1, 3) + c1 * pow(t - 1, 2);
        return p;
    },
    easeOut_Bounce: function (t) {

        const n1 = 7.5625;
        const d1 = 2.75;

        if (t < 1 / d1) {
            t = n1 * t * t;
        } else if (t < 2 / d1) {
            t = n1 * (t -= 1.5 / d1) * t + 0.75;
        } else if (t < 2.5 / d1) {
            t = n1 * (t -= 2.25 / d1) * t + 0.9375;
        } else {
            t = n1 * (t -= 2.625 / d1) * t + 0.984375;
        }
        let p = t;
        return p;
    },
    easeInOut_Sine: function (t) {

        let p = -(cos(PI * t) - 1) / 2;
        return p;
    },
    easeInOut_Cubic: function (t) {

        let p = t < 0.5 ? 4 * t * t * t : 1 - pow(-2 * t + 2, 3) / 2;
        return p;
    },
    easeInOut_Quint: function (t) {

        let p = t < 0.5 ? 16 * t * t * t * t * t : 1 - pow(-2 * t + 2, 5) / 2;
        return p;
    },
    easeInOut_Circ: function (t) {

        let p = t < 0.5
            ? (1 - sqrt(1 - pow(2 * t, 2))) / 2
            : (sqrt(1 - pow(-2 * t + 2, 2)) + 1) / 2;
        return p;
    },
    easeInOut_Elastic: function (t) {

        const c5 = (2 * PI) / 4.5;
        let p = t === 0
            ? 0
            : t === 1
                ? 1
                : t < 0.5
                    ? -(pow(2, 20 * t - 10) * sin((20 * t - 11.125) * c5)) / 2
                    : (pow(2, -20 * t + 10) * sin((20 * t - 11.125) * c5)) / 2 + 1;
        return p;
    },
    easeInOut_Quad: function (t) {

        let p = t < 0.5 ? 2 * t * t : 1 - pow(-2 * t + 2, 2) / 2;
        return p;
    },
    easeInOut_Quart: function (t) {

        let p = t < 0.5 ? 8 * t * t * t * t : 1 - pow(-2 * t + 2, 4) / 2;
        return p;
    },
    easeInOut_Expo: function (t) {

        let p = t === 0
            ? 0
            : t === 1
                ? 1
                : t < 0.5 ? pow(2, 20 * t - 10) / 2
                    : (2 - pow(2, -20 * t + 10)) / 2;
        return p;
    },
    easeInOut_Back: function (t) {

        const c1 = 1.70158;
        const c2 = c1 * 1.525;

        let p = t < 0.5
            ? (pow(2 * t, 2) * ((c2 + 1) * 2 * t - c2)) / 2
            : (pow(2 * t - 2, 2) * ((c2 + 1) * (t * 2 - 2) + c2) + 2) / 2;
        return p;
    },
    easeInOut_Bounce: function (t) {

        const n1 = 7.5625;
        function easeOutBounce(t) {
            const d1 = 2.75;

            if (t < 1 / d1) {
                return n1 * t * t;
            } else if (t < 2 / d1) {
                return n1 * (t -= 1.5 / d1) * t + 0.75;
            } else if (t < 2.5 / d1) {
                return n1 * (t -= 2.25 / d1) * t + 0.9375;
            } else {
                return n1 * (t -= 2.625 / d1) * t + 0.984375;
            }
        }

        let p = t < 0.5
            ? (1 - easeOutBounce(1 - 2 * t)) / 2
            : (1 + easeOutBounce(2 * t - 1)) / 2;
        return p;
    }
}
export { easing }